package com.itheima.demo;

public class BinarySearchDemo {
    public static void main(String[] args) {
        int[]   arrs = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
        int number = 13;

        int index = getBinarySearch(arrs,number);

        System.out.println(index);
    }

    private static int getBinarySearch(int[] arrs, int number) {
        //两个坐标，min是数组开头。max是末尾
        int max = arrs.length-1;
        int min = 0;

        //如果min>max 表示该数组没有要查找的数据，就返回-1
        while (min<=max){
            //中间坐标
            int mid = (max+min)/2;
            if (arrs[mid]<number){
                //mid处的值小于查找的值，那么就舍弃左边
                min = mid+1;
            }else if (arrs[mid]>number){
                max = mid-1;
            }else{
                return mid;
            }
        }
        return -1;
    }
}
